package com.changgou.search.controller;

import com.changgou.entity.Page;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    //localhost:9009/search?keywords=手机&brand=华为
    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String, String> searchMap) {
        //特殊符号处理
        this.handlerSearchMap(searchMap);

        Map resultMap = searchService.search(searchMap);
        return resultMap;
    }

    private void handlerSearchMap(Map<String, String> searchMap) {
        //searchMap 键值对
        Set<Map.Entry<String, String>> entries = searchMap.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            if (entry.getKey().startsWith("spec_")) {
                String value = entry.getValue();
                value = value.replace("+", "%2B");
                //替换原来有"+"的值
                searchMap.put(entry.getKey(), value);
            }
        }
    }

    //localhost:9009/search/list?keywords=手机&brand=华为&spec_网络制式=电信3G&spec_颜色=红色&price=500-1000
    @GetMapping("/list")
    public String list(@RequestParam Map<String, String> searchMap, Model model) {
        //特殊符号处理
        this.handlerSearchMap(searchMap);

        //打印searchMap
        Set<String> keys = searchMap.keySet();
        for (String key : keys) {
            System.out.println("searchMap中的数据:" + key + ":" + searchMap.get(key));
        }

        //获取查询结果
        Map resultMap = searchService.search(searchMap);
        model.addAttribute("result", resultMap);
        //用户的搜索条件也放入页面的返回数据
        model.addAttribute("searchMap", searchMap);

        //给前端拼接一个url
        StringBuilder url = new StringBuilder("/search/list");

        if (searchMap != null && searchMap.size() > 0) {
            url.append("?");
            for (String key : searchMap.keySet()) {
                //key:keywords  value:手机
                String value = searchMap.get(key);
                //特殊情况
                if (!key.equals("sortField") && !key.equals("sortRule") && !key.equals("pageNum")) {
                    url.append(key).append("=").append(value).append("&");
                }
            }
            String queryUrl = url.toString();
            //去除最后一个"&"
            queryUrl = queryUrl.substring(0, queryUrl.length() - 1);
            model.addAttribute("url", queryUrl);
        } else {
            model.addAttribute("url", url);
        }

        //封装分页数据并返回
        Page<SkuInfo> page = new Page<>(
                //1.总记录数
                Long.parseLong(String.valueOf(resultMap.get("total"))),
                //2.当前页
                Integer.parseInt(String.valueOf(resultMap.get("pageNum"))),
                //3.每页显示多少条
                Page.pageSize
        );
        model.addAttribute("page", page);

        return "search";
    }
}
